import Vue from "vue";
import VueRouter from "vue-router";

// 组件引入
import Login from '@/views/login.vue';
import Register from '@/views/register.vue';
import Personal from '@/views/personal/personal.vue';
import editPersonal from '@/views/personal/editPersonal.vue';

Vue.use(VueRouter);



// 创建router
const router = new VueRouter({
    routes: [{
        name: 'Login'
        , path: '/login'
        , component: Login,
    },
    {
        name: "register"
        , path: "/register"
        , component: Register,
    },
    {
        name: 'personal'
        , path: '/personal/:id'
        , component: Personal
    },
    {
        name: 'editPersonal'
        , path: '/editPersonal/:id'
        , component: editPersonal
    }
    ]
})

//导航守卫 
router.beforeEach((to, from, next) => {
    // 没有token进入的还是个人中心页面 判断转为登录页面
    const token = localStorage.getItem('vueToken')
    if (to.name == 'personal' && !token) {
        next({ path: '/login' });
    } else {
        next()
    }
})

export default router;